// KetonalConfig.cpp: implementation of the KetonalConfig class.
//
//////////////////////////////////////////////////////////////////////

#include "KetonalConfig.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

KetonalConfig::KetonalConfig(XmlElement *_root, File *_f)
{
	rootElement = 0;
	i			= 0;
	isRunning	= false;
	xmlFormData	= 0;
	configFile	= _f;

	rootElement = new XmlElement (*_root);
	
	forEachXmlChildElement (*rootElement, e)
	{
		if (e->hasTagName (T("image")))
		{
			MemoryBlock *mb = new MemoryBlock (e->getIntAttribute (T("data-base64-size")));
			if (mb)
			{
				if (mb->fromBase64Encoding (e->getStringAttribute(T("data-base64"))))
				{
					i = ImageFileFormat::loadFrom (mb->getData(), mb->getSize());
				}
			}
		}
		if (e->hasTagName (T("HTMLForm")))
		{
			xmlFormData = e;
		}
		if (e->hasTagName (T("url")))
		{
			url = e->getStringAttribute (T("url"));
		}
	}
}

KetonalConfig::~KetonalConfig()
{
	if (rootElement)
		deleteAndZero (rootElement);
}

String KetonalConfig::getUrl()
{
	return (url);
}

String KetonalConfig::getProxyUrl()
{
	return (String::empty);
}

int KetonalConfig::getProxyType()
{
	if (proxyType == T("HTTP"))
	{
		return (0);
	}
	if (proxyType == T("SOCKS5"))
	{
		return (1);
	}
	if (proxyType == T("SOCKS4"))
	{
		return (2);
	}
	if (proxyType == T("SOCKS4a"))
	{
		return (3);
	}

	return (0);
}

Image *KetonalConfig::getImage()
{
	return (i);
}

void KetonalConfig::setRunning(bool p)
{
	isRunning = p;
}

bool KetonalConfig::getRunning()
{
	return (isRunning);
}

void KetonalConfig::setPosition (Rectangle r)
{
	position = r;
}

Rectangle *KetonalConfig::getPosition ()
{
	return (&position);
}

XmlElement *KetonalConfig::getFormData()
{
	return (xmlFormData);
}

void KetonalConfig::setFormData (XmlElement *e)
{
	if (xmlFormData)
		deleteAndZero (xmlFormData);

	xmlFormData = new XmlElement (*e);
}

bool KetonalConfig::isProxyUsed ()
{
	return (useProxy);
}